<?php
class FbAction extends LoginAction
{
    public function floor ()
    {
        $id = getGet('id');
        $floor = $this->setFloorData($id);
        $market = $this->setMarketData($floor['mid']);
        $FloorBrand = D('FloorBrand');
        $fb = $FloorBrand->getFbAll($floor['mid'], $floor['id']);
        $this->assign('fb', $fb);
        $this->display();
    }
    /**
     * 执行楼层品牌添加
     * @param Int $id 商场ID
     * @author Ice iceinto@mallog.com.cn
     */
    public function add ()
    {
        $id = getGet('id');
        $floor = $this->setFloorData($id);
        $market = $this->setMarketData($floor['mid']);
        $this->setFbtData($market['id'], $floor['id']);
        $this->setBrandData();
        if (isPost()) {
            $data = $this->getData();
            $FloorBrand = D('FloorBrand');
            $status = $FloorBrand->fbAdd($data);
            if ($status['status'] == 1) {
                $this->successMessage('添加成功！');
            } else {
                $this->errorMessage($status['doc']);
            }
        }
        $this->display();
    }
    /**
     * 执行楼层品牌修改
     * @author Ice <iceinto@mallog.com.cn>
     */
    public function edit ()
    {
        $FloorBrandModel = D('FloorBrand');
        $BrandModel = D('Brand');
        $id = intval(getGet('id'));
        $map['id'] = $id;
        $floorBrand = $FloorBrandModel->where($map)->find();
        if ($floorBrand) {
            $floor = $this->setFloorData($floorBrand['fid']);
            $market = $this->setMarketData($floorBrand['mid']);
            $this->setFbtData($floorBrand['mid'], $floorBrand['fid']);
            //获取品牌名称
            $brand = $BrandModel->field('name')->find(
            $floorBrand['bid']);
            $this->assign('brand', $brand);
            $this->assign('fb', $floorBrand);
        }
        if (isPost()) {
            $data = array();
            $data['id'] = $id;
            $data['tid'] = getPost('fbt');
            $model = intval(getPost('model'));
            $model = $model ? $model : 1;
            $data['model'] = $model;
            $this->createUpdate('FloorBrand', $data, false);
        }
        $this->display();
    }
    /**
     * 设置楼层品牌
     * @author Ice iceinto@mallog.com.cn
     */
    public function setBrandData ()
    {
        $BrandModel = M('Brand');
        $brand = $BrandModel->order('initials ASC')->select();
        if ($brand) {
            $this->assign('brand', $brand);
        } else {
            $this->error('请先添加品牌！');
        }
    }
    /**
     * 设置楼层品牌类型
     * @param Int $id 类型ID
     * @author Ice iceinto@mallog.com.cn
     */
    public function setFbtData ($mId, $fId)
    {
        $FbtModel = M('FloorBrandType');
        $map = array();
        $map['mid'] = $mId;
        $map['fid'] = $fId;
        $fbt = $FbtModel->where($map)->select();
        if ($fbt) {
            $this->assign('fbt', $fbt);
        } else {
            $this->error('楼层类型数据传递有错误！');
        }
    }
    /**
     * 设置
     * @param Int $id 商场ID
     * @author Ice iceinto@mallog.com.cn
     */
    public function setMarketData ($id)
    {
        $MarketModel = M('Market');
        $map = array();
        $map['id'] = $id;
        $market = $MarketModel->where($map)->find();
        if ($market) {
            $this->assign('market', $market);
            return $market;
        } else {
            $this->error('商场数据传递有错误！');
        }
    }
    /**
     * 设置和判断楼层参数
     * @param Int $id 楼层ID
     * @author Ice iceinto@mallog.com.cn
     */
    public function setFloorData ($id)
    {
        $FloorModel = M('Floor');
        $map = array();
        $map['id'] = $id;
        $floor = $FloorModel->where($map)->find();
        if ($floor) {
            $this->assign('floor', $floor);
            return $floor;
        } else {
            $this->error('楼层数据传递有错误！');
        }
    }
    /**
     * 获取提交数据
     * @author Ice iceinto@mallog.com.cn
     */
    public function getData ()
    {
        $data = array();
        $data['mid'] = getPost('mid');
        $data['fid'] = getPost('fid');
        $data['bid'] = getPost('brand');
        $data['tid'] = getPost('fbt');
        //判断必要性
        foreach ($data as $k => $d) {
            if (! $d) {
                $this->error($k . '不能为空！');
            }
        }
        return $data;
    }
    public function get ()
    {
        $FbModel = M('FloorBrand');
        $fid = getGetOrPost('fid');
        $map = array();
        if ($fid) {
            $map['fid'] = $fid;
            $data = $FbModel->where($map)
                ->field('m_floor_brand.fid as fid,m_brand.name as name,m_brand.id as id')
                ->join('INNER JOIN m_brand ON m_floor_brand.bid = m_brand.id')
                ->select();
            $this->ajaxReturn($data, null, 1);
        }
    }
}
?>